package com.chuanyang.driverh.utils;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {
	
	public static double changeToNumberDicemal1(double value,int number){
		return new BigDecimal(value).setScale(number, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	/**
	 * 判断字符串是否为空或者空字符串 如果字符串是空或空字符串则返回true，否则返回false
	 * 
	 * @return
	 */
	public static boolean isBlank(String str) {
		if (str == null || "".equals(str) || "null".equals(str)) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 * Double保留3位小数
	 * @return
	 */
	public static String getDouble(String num){
		if (isBlank(num))return "";
		Double d=Double.valueOf(num);
		DecimalFormat    df   = new DecimalFormat("######0.000");
		return df.format(d);
		
	}
	/**
	 * Double保留3位小数,过万显示万级别
	 * @return
	 */
	public static String getWeight(String num){
		Double d=Double.valueOf(num);
		DecimalFormat    df   = new DecimalFormat("######0.000");
		if (d>100000000){
			return df.format(d/100000000)+"亿";
		}else if(d>10000){
			return df.format(d/10000)+"万";
		}
		return df.format(d);

	}


	/**
	 * Double保留2位小数
	 * @return
	 */
	public static String getTwoNum(String num){
		Double d=Double.valueOf(num);
		DecimalFormat    df   = new DecimalFormat("######0.00");
		return df.format(d);

	}



	/**
	 * Double保留1位小数
	 * @return
	 */
	public static String getSingle(String num){
		Double d=Double.valueOf(num);
		DecimalFormat    df   = new DecimalFormat("######0.0");
		return df.format(d);
		
	}


	public static String getMinSec(String time){
		if (isBlank(time)){
			return "";
		}

		long curTime=System.currentTimeMillis()/1000;
		long lastTime=Long.valueOf(time)+15*60;
		long dis=lastTime-curTime;
		if (dis<=0){
			return "已取消";
		}

		long min=dis/60;
		long sec=dis%60;
		if (min<10){
			if (sec<10){
				return "0"+min+"分"+"0"+sec+"秒";
			}else{
				return "0"+min+"分"+sec+"秒";
			}
		}else{
			if (sec<10){
				return min+"分"+"0"+sec+"秒";
			}else{
				return min+"分"+sec+"秒";
			}
		}

	}

	/**
	 * 158****8364
	 * @param mobile
	 * @return
	 */
	public static String changeMobileShow(String mobile){

		if (StringUtil.isBlank(mobile))return "";
		StringBuilder sb=new StringBuilder(mobile);
		sb.replace(3, 7, "****");
		return sb.toString();
	}
	
	/**
	 * 判断是否是手机号码(可以细化手机号码的前缀)
	 * @param str
	 * @return
	 */
	public static boolean isPhoneNumber(String str){
		Pattern pattern = Pattern.compile("^1[34578]\\d{9}$");
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}
	
	
	/**
	 * 生成随机的4位数字验证码
	 * @return
	 */
	public static String getCode(){
		Random random=new Random();
		return random.nextInt(10)+""+random.nextInt(10)+""+random.nextInt(10)+""+random.nextInt(10);
	}



	
	/**
	 * 邮箱的正则判断
	 * @param str
	 * @return
	 */
	public static boolean isEmail(String str){
		String regex ="^[a-zA-Z0-9]+([._\\-]*[a-zA-Z0-9])*@([a-zA-Z0-9]+[-a-zA-Z0-9]*[a-zA-Z0-9]+.){1,63}[a-zA-Z0-9]+$";
		return str.matches(regex);
	}
	
	/**
	 * 密码格式的正则判断
	 * 6-18位字符 不能还有空格
	 * @param pwd
	 * @return
	 */
	public static boolean checkPwd(String pwd){
		return pwd.contains(" ")?false:pwd.matches("^.{6,18}$");
	}
	
	 /**
     * 银行卡卡号校验
     * @param cardId  
     * @return
     */
    public static boolean checkBankCardNumber(String cardId) {
             char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
             if(bit == 'N'){
                 return false;
             }
             return cardId.charAt(cardId.length() - 1) == bit;
    }  

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId){
        if(nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            //如果传的不是数据返回N
            return 'N';
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for(int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if(j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;           
        }
        return (luhmSum % 10 == 0) ? '0' : (char)((10 - luhmSum % 10) + '0');
    }
    
    /**
	 * 身份证格式校验
	 */
	public static boolean isCard(String id) {
		if (!TextUtils.isEmpty(id)) {
			return new IDCheckUtil().verify(id);
		} else {
			return false;
		}

	}
    
    /**
	 * 身份证号码显示处理
	 * @return
	 */
	public static String handleIdNumShow(String id){
		String newId = null;
		if(id.length() == 18){
			newId = id.substring(0, 6)+"********"+id.substring(14,18);
		}else{
			newId = id;  //15位的直接显示?
		}
		return newId;
	}
	
    /**
	 * 手机号码显示处理
	 * @return
	 */
	public static String handlePhoneNumShow(String id){
		try {
			String newId = null;
			if(id.length() >7 )
                newId = id.substring(0, 3)+"****"+id.substring(id.length()-4,id.length());
			return newId;
		} catch (Exception e) {
			e.printStackTrace();
			return id;
		}
	}


	public static void checkPwdInput(Context context,Editable edt) {
		try {
			String temp = edt.toString();
			if (temp.length()==0){
				return;
			}
			int mid = temp.charAt(temp.length()-1);
			if (mid >= 48 && mid <= 57) {//数字
				return;
			}
			if (mid >= 65 && mid <= 90) {//大写字母
				return;
			}
			if (mid >= 97 && mid <= 122) {//小写字母
				return;
			}
			if (mid == 95) {//下划线
				return;
			}
			edt.delete(temp.length() - 1, temp.length());
			PrintUtil.toast(context, "密码只能是数字字母下划线");

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private static String result[];
	private static String units[] = {"", "十", "百", "千", "万", "十", "百", "千", "亿"};

	private static String nums[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"};
	/**
	 * 数字转化为汉字 "1"转“—”
	 */
	public static String changeNumberToChart(int number) {
		String out = "";
		String input = number + "";
		result = new String[input.length()];
		for (int i = 0; i < result.length; i++) {
			result[i] = String.valueOf(input.charAt(i));
		}
		int back = 0;
		for (int i = 0; i < result.length; i++) {
			if (!result[i].equals("0")) {
				back = result.length - i - 1;
				out += nums[Integer.parseInt(result[i])];
				out += units[back];
			} else {
				if (i == result.length - 1) {

				} else {
					if (!result[i + 1].equals("0")) {
						out += nums[0];
					}
				}
			}
		}
		return out;

	}


	/**
	 * 传入省市区 如果省和市相同则返回省，区
	 */
	public static String getRemoveCity(String province,String city,String region)
	{
		if(province.equals(city))
		{
			return  province+region;
		}
		return  province+city+region;
	}
	/**
	 * 传入省市区 如果详细地址包含省市区，则去除相同内容
	 */
	public static String getRemoveAdd(String province,String city,String region,String address) {
		String pcr1=province+city+region;
		String pcr2=province+region;
		if(address.contains(pcr1))
		{
			return address.replace(pcr1,"");
		}
		else if(address.contains(pcr2))
		{
			return address.replace(pcr2,"");

		}
		return address;
	}

	/**
	 * 如果地址中含有自治区则去除
	 */

	public static String getSimpleMunicipality(String municipality) {
		if (municipality.equals("内蒙古自治区")) {
			return "内蒙古";

		} else if (municipality.equals("新疆维吾尔自治区")) {
			return "新疆";

		} else if (municipality.equals("广西壮族自治区")) {
			return "广西";

		} else if (municipality.equals("宁夏回族自治区")) {
			return "宁夏";

		} else if (municipality.equals("西藏自治区")) {
			return "西藏";
		} else if (municipality.contains("特别行政区")) {
			return municipality.substring(0, 2);
		}


		return municipality;

	}

	public static String getString(List<String> list) {
			StringBuilder	builder = new StringBuilder();
		for (String time : list) {
			builder.append(time + "");
		}
		return builder.toString();
	}

//字符串拼接
	public static String getSplitString(List<Integer> list) {
		if (list.size()<=0)return "";
		StringBuilder	builder = new StringBuilder();

		for (Integer time : list) {
			builder.append(time + ",");
		}
		String temp=builder.toString();
		return temp.substring(0,temp.length()-1);
	}

	//字符串拼接
	public static String getStatus(List<Integer> list) {
		if (list.size()<=0)return "";
		StringBuilder	builder = new StringBuilder();

		for (Integer time : list) {
			if (time==1){
				builder.append(1+ ",");
				builder.append(2+ ",");
				builder.append(3+ ",");
			}else if(time==2){
				builder.append(4+ ",");
				builder.append(5+ ",");
			}else if(time==3){
				builder.append(6+ ",");
			}else if(time==4){
				builder.append(7+ ",");
			}
		}
		String temp=builder.toString();
		return temp.substring(0,temp.length()-1);
	}
}
